// TestClientManager.cpp - Attaches and tests linux advanced sound architecture library

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#include <unistd.h>
#include <sys/time.h>
//#include "UILogger.h"
//#include "UIEventRouter.h"
//#include "UIClientManager.h"
#include "AlsaPlayers.h"
#include "AlsaRecorders.h"

void getSysTime( char* pszTime)
{
    time_t t = time(0);   // get time now
    struct tm * now = localtime( & t );
    sprintf( pszTime, "%02i:%02i:%02i", now->tm_hour, now->tm_min, now->tm_sec);
}

struct timeval CalcDiffTime( timeval begin, timeval end)
{
	struct timeval difference;
	difference.tv_sec  = end.tv_sec  - begin.tv_sec;
	difference.tv_usec = end.tv_usec - begin.tv_usec;

	// Perform the carry for the later subtraction by updating end
	if (begin.tv_usec < end.tv_usec) {
		int nsec = (end.tv_usec - begin.tv_usec) / 1000000 + 1;
		end.tv_usec -= 1000000 * nsec;
		end.tv_sec += nsec;
	}
	if (begin.tv_usec - end.tv_usec > 1000000) {
		int nsec = (end.tv_usec - begin.tv_usec) / 1000000;
		end.tv_usec += 1000000 * nsec;
		end.tv_sec -= nsec;
	}

	difference.tv_sec  = end.tv_sec  - begin.tv_sec ;
	difference.tv_usec = end.tv_usec - begin.tv_usec;

	while (difference.tv_usec < 0) {
		difference.tv_usec += 1000000;
		difference.tv_sec  -= 1;
	}

	return (difference);
}


int main(int argc, char **argv)
{
    char cwd[1024] = {0};
    if (getcwd((char *)&cwd, sizeof(cwd)) != NULL)
        fprintf(stdout, "Current working dir: %s\n", cwd);

//  UILogger::create(LOGGER_LOG4CXX, "../Common/Resources/Logging/logging-ClientManager.config");
//	UIConfigResourceManager::create("../Common/Resources/main-SmartHome.config", "./usersetting.config");

	// Record and play a wave file
	if ( 1) {
	    AlsaPcmFilePlayer sound;
		sound.Play((char *)"sound.raw");
	}

	if ( 1) {
		AlsaWaveFilePlayer sound;
		sound.Play((char *)"sound.wav");
	}

    printf( "Starting Audio Capture. (Press q to Stop). Saving raw audio to capture raw \n");

	if ( 1) {
	    char szStart[20];
	    char szEnd[20];
	    AlsaPcmRecorder recorder;
	    recorder.StartAudioCapture();
	    getSysTime( szStart);

	    int iTrigger=0;
	    while (1) {
			char t;
			scanf("%c", &t);
			if (t == 'q') {
				recorder.EndAudioCapture();
				getSysTime( szEnd);
				printf( "Recorded Time: %s -> %s\n", szStart, szEnd);
				break;
			}
			if (t == 't') {
				iTrigger++;
				unsigned char buf[128000]; // enough for 4sec
				int nRead = recorder.GetPrevRecording( 2, buf);
				char filename[20];
				sprintf( filename, "tigger%i.raw", iTrigger);
				FILE *fileTrigger = fopen( filename, "w+b");
				fwrite( buf, sizeof(char), nRead, fileTrigger);
				fflush( fileTrigger);
				fclose( fileTrigger);
			}
	    }

	    AlsaPcmFilePlayer player;
	    struct timeval begin, end;

	    gettimeofday(&begin, NULL);
	    player.Play((char *)"capture.raw");
	    gettimeofday(&end, NULL);

		struct timeval difference = CalcDiffTime(begin, end);
		printf("Time to play: %d.%d seconds\n", (int)difference.tv_sec, (int)difference.tv_usec);
	}

	// Display sound device information
//	sound.device_list();
//	sound.pcm_list();

	/*
	UIClientManager client;
	UIEvent event;

	event.setSender(MODULE_ClientManager);
	event.setReciever(MODULE_ClientManager);
	event.setType("TEST");
	event.setValue("");

	char t;
	scanf("%c", &t);
	while (t != 'q') {
		switch (t) {
			case 's':
				UIEventRouter::getInstance()->sendEvent(&event);
				break;
			default:
				break;
		}
		result = scanf("%c", &t);
		if (result) return 1;
	}

	UIConfigResourceManager::release();
	UIEventRouter::release();
	UILogger::release();
	*/
}
